/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package helpers;

//import edu.itu.itfriendu.login.LoginBean;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

/**
 *
 * @author luci
 */
public class Utils {
    private static Logger logger = LogManager.getLogger(Utils.class.getName());
    
    public static Connection getConnection() throws SQLException{
        Context initCtx, envContext; 
        Connection conn = null;
        try{
            initCtx = new InitialContext();
            envContext = (Context) initCtx.lookup("java:comp/env");
            DataSource ds = (DataSource)envContext.lookup("jdbc/itfriendu");

            conn = ds.getConnection();             
        }catch(NamingException ex){
           logger.error("Error with Dependency injection for getting DB !", ex);
            
        }catch (SQLException ex){
           logger.error("Error with getting database connection!", ex);
           throw ex;
        } 
        return conn;
    }   
}
